﻿var examtxt = null, examroomtxt = null;

//加载所有数据
function LoadAllExam(t) {
    $("#hidKeCheng").val(t);
    var url = "Exam" + t + "/ExamRoom.txt";
    $.ajax({
        type: "GET",
        url: url,
        dataType: "json",
        data: { action: "GetNewExam" },
        success: function (list) {
            examroomtxt = list;
            CreateExamRoomMenu(list);
        }
    });
    url = "Exam" + t + "/Exam.txt";
    $.ajax({
        type: "GET",
        url: url,
        dataType: "json",
        data: { action: "GetNewExam" },
        success: function (list) {
            examtxt = list;
            CreateMenu(list);
        }
    });
}

//生成菜单框架
function CreateExamMenu(t, r) {
    $("#hidExams").val(t);
    $("#hidExamRoomID").val("1");
    var menulistHtml = "";
    for (var i = 0; i < r; i++) {
        menulistHtml += "<dd><dl>";
        if (t == (i + 1)) {
            menulistHtml += "<dt class=\"current\">课程" + (i + 1) + "</dt>";
            menulistHtml += "<dd><dl id=\"moniti\"></dl></dd>";
            menulistHtml += "<dd><dl id=\"zhengti\"></dl></dd>";
        }
        else {
            menulistHtml += "<dt class=\"btnDiv\" onclick=\"CreateExamMenu(" + (i + 1) + "," + r + ");\">课程" + (i + 1) + "</dt>";
        }
        menulistHtml += "</dl></dd>";
    }
    $("#menulist").html(menulistHtml);
    LoadAllExam(t);
}

//创建真题菜单
function CreateExamRoomMenu(list) {
    var zhengti1Html = "";
    var cid = $("#hidExamRoomID").val();
    if (list != null && list.length > 0) {
        zhengti1Html = "<dt class=\"class_category class_category1\" id=\"monikaochang\" style=\"cursor: pointer;\"><img src=\"images/sers/class_nav_item_icon1.jpg\" />真题练习</dt>";
        for (var i = 0; i < list.length; i++) {
            if (list[i].id == cid) {
                zhengti1Html += "<dd name=\"moniti\" class=\"current\" pid=\"" + list[i].id + "\" style=\"cursor: pointer;\" onclick=\"GetNewExamRoom(this," + list[i].id + ", 1,2);\">■ " + list[i].id + "年真题练习</dd>";
            }
            else {
                zhengti1Html += "<dd name=\"moniti\" pid=\"" + list[i].id + "\" style=\"cursor: pointer;\" onclick=\"GetNewExamRoom(this," + list[i].id + ", 1,2);\">■ " + list[i].id + "年真题练习</dd>";
            }
        }
    }
    ;
    $("#zhengti").html(zhengti1Html);
}

//创建模拟题菜单
function CreateMenu(list) {
    var cid = $("#hidExamRoomID").val();
    if (list != null && list.length > 0) {
        var ids = "";
        var monitiHtml = "<dt class=\"class_category class_category1\" id=\"monikaochang\" style=\"cursor: pointer;\"><img src=\"images/sers/class_nav_item_icon1.jpg\" />模拟考场</dt>";
        for (var i = 0; i < list.length; i++) {
            var dclass = "";

            if (list[i].id == cid) {
                monitiHtml += "<dd name=\"moniti\" class=\"current\" pid=\"" + list[i].id + "\" style=\"cursor: pointer;\" onclick=\"GetNewExamRoom(this," + list[i].id + ", 1,1);\">■ 模拟题" + list[i].id + "</dd>";
            }
            else {
                monitiHtml += "<dd name=\"moniti\" pid=\"" + list[i].id + "\" style=\"cursor: pointer;\" onclick=\"GetNewExamRoom(this," + list[i].id + ", 1,1);\">■ 模拟题" + list[i].id + "</dd>";
            }
            if (i == 0) {
                ids = list[i].id;
                $("#hidExamRoomID").val(list[0]["id"]);
            }
            else {
                ids += "|" + list[i]["id"];
            }
        }
        $("#moniti").html(monitiHtml);
        $("#hidExamIdList").val(ids);
        $("#hidExamMaxId").val(list[list.length - 1]["id"]);
        GetNewExam(cid, 1);
    }
}

//获取题库
function GetExamRoom(id, t) {
    var result = null;
    var list = examtxt;
    var exam = $("#hidExams").val();
    if (exam == "2") {
        list = examroomtxt;
        for (var i = 0; i < list.length; i++) {
            var cid = list[i].id;
            if (id == cid) {
                result = list[i];
            }
        }
    }
    else {
        for (var i = 0; i < list.length; i++) {
            var cid = list[i].id;
            if (id == cid) {
                result = list[i];
            }
        }
    }
    if (result == null) {
        result = list[0];
    }
    GetExamInfo(result, t);
}

//生成模拟题或真题
function GetNewExamRoom(obj, id, t, exam) {
    $("#hidExams").val(exam);
    $("#moniti dd").removeClass("current");
    $("#zhengti dd").removeClass("current");
    $(obj).addClass("current");
    GetNewExam(id, t);
}

//生成题库并获取
function GetNewExam(id, t) {
    if (t == null || t < 1) {
        t = 1;
    }
    $("#titleid").html("第" + t + "题");
    $("#hidQuestionID").val(t);
    $("#hidExamRoomID").val(id);
    GetExamRoom(id, t);
}

//清除模拟题做题信息
function ClearNewExam(id) {
    var list = examtxt;
    for (var i = 0; i < list.length; i++) {
        var exams = list[i];
        if (exams.id == id) {
            exams.ifsubmit = 0;
            exams.usetime = 0;
            for (var j = 0; j < exams.list.length; j++) {
                var result = exams.list[j];
                result.chooseanswer = "";
                result.notebook = "";
                result.ifcollect = 0;
                result.errorbook = "";
                result.usetime = 0;
            }
        }
    }
    examtxt = list;
}

//清除真题做题信息
function ClearNewExamT(id) {
    var list = examroomtxt;
    for (var i = 0; i < list.length; i++) {
        var exams = list[i];
        if (exams.id == id) {
            exams.ifsubmit = 0;
            exams.usetime = 0;
            for (var j = 0; j < exams.list.length; j++) {
                var result = exams.list[j];
                result.chooseanswer = "";
                result.notebook = "";
                result.ifcollect = 0;
                result.errorbook = "";
                result.usetime = 0;
            }
        }
    }
    examroomtxt = list;
}

//清除题库
function DelNewExam(id) {
    ClearNewExam(id);
    GetNewExam(id, 1);
}

//获取信息
function GetExamInfo(jsonData, t) {
    var dataObj = jsonData;  //eval("(" + jsonData + ")"); //转换为json对象
    var strHtml = "";
    var gongjitishu = 100;
    var danqianyongshi = "00:00:00";
    if (dataObj != null) {
        var ifsubmit = dataObj["ifsubmit"];
        $("#hidIfSubmit").val(ifsubmit);
        var list = dataObj["list"];
        GetNumberHtml(list, t, ifsubmit);
        gongjitishu = list.length;
        for (var i = 0; i < list.length; i++) {
            var cid = list[i]["id"];
            var ifcollect = list[i]["ifcollect"];
            if (t == cid) {
                $("#hidFavorite").val(ifcollect);
                if (ifcollect == "0") {
                    $("#favorite").html("<img src=\"images/sers/collection_no.jpg\" />未收藏");
                    $("#favoritebtn").attr("src", "images/sers/collection_btn.jpg");
                }
                else {
                    $("#favorite").html("<img src=\"images/sers/collection.jpg\" />已收藏");
                    $("#favoritebtn").attr("src", "images/sers/collection_no_btn.jpg");
                }
                strHtml += "<ul><li class=\"dc1_queitem\"><h2 class=\"blue\"><span class=\"dc1_quen\"></span>";
                var title = list[i]["title"];
                var usetime = list[i]["usetime"];
                var notebook = list[i]["notebook"];
                var errorbook = list[i]["errorbook"];
                if (notebook != null && notebook != "") {
                    $("#txtNote").val(notebook);
                }
                else {
                    $("#txtNote").val("");
                }
                if (errorbook != null && errorbook != "") {
                    $("#txtError").val(errorbook);
                }
                else {
                    $("#txtError").val("");
                }
                var flog = list[i]["flog"];
                var question = list[i]["question"];
                var chooseanswer = list[i]["chooseanswer"];
                var usetime = list[i]["usetime"];
                var trueanswer = list[i].trueanswer;
                var answertrue = "";
                if (trueanswer == "1") {
                    if (flog == 0) {
                        answertrue = "A";
                    }
                    if (flog == 1) {
                        answertrue = "错";
                    }
                }
                if (trueanswer == "2") {
                    if (flog == 0) {
                        answertrue = "B";
                    }
                    if (flog == 1) {
                        answertrue = "对";
                    }
                }
                if (trueanswer == "3") {
                    answertrue = "C";
                }
                if (trueanswer == "4") {
                    answertrue = "D";
                }
                var answerdetail = list[i].answerdetail;
                var details = "您选择的答案是";
                if (chooseanswer == "1") {
                    if (flog == 0) {
                        question = question + "(A)";
                        details += " A ";
                    }
                }
                if (chooseanswer == "2") {
                    if (flog == 0) {
                        question = question + "(B)";
                        details += " B ";
                    }
                }
                if (chooseanswer == "3") {
                    question = question + "(C)";
                    details += " C ";
                }
                if (chooseanswer == "4") {
                    question = question + "(D)";
                    details += " D ";
                }
                if (chooseanswer != null && chooseanswer != "") {
                    if (chooseanswer == trueanswer) {
                        details += " 您选择的答案正确。";
                    }
                    else {
                        details += " 您选择的答案错误。正确答案：" + answertrue + " 说明：" + answerdetail;
                    }
                }
                else {
                    details = "该题未选择答案。正确答案：" + answertrue + "说明：" + answerdetail;
                }
                strHtml += question + "</h2><div class=\"dc1_queoption\" id=\"div3\">";
                var answerImg = "";
                var answer = list[i]["answer"];
                for (var j = 0; j < answer.length; j++) {
                    if (flog == 0) {
                        strHtml += answer[j] + "<br />";
                        answerImg += "<img src=\"images/sers/result_" + j + ".jpg\" alt=\"a\" onclick=\"Answer('" + (j + 1) + "');\" style=\"cursor: pointer;\" />&nbsp;";
                    }
                    if (flog == 1) {
                        if (chooseanswer == "1" && j == 0) {
                            strHtml += "<input name=\"chooseanswer\" type=\"radio\" checked=\"checked\" onclick=\"Answer('" + (j + 1) + "');\" />" + answer[j] + "<br />";
                        }
                        else if (chooseanswer == "2" && j == 1) {
                            strHtml += "<input name=\"chooseanswer\" type=\"radio\" checked=\"checked\" onclick=\"Answer('" + (j + 1) + "');\" />" + answer[j] + "<br />";
                        }
                        else {
                            strHtml += "<input name=\"chooseanswer\" type=\"radio\" onclick=\"Answer('" + (j + 1) + "');\" />" + answer[j] + "<br />";
                        }
                    }
                }
                if (ifsubmit == 1) {
                    var h = parseInt(usetime / 3600);
                    usetime = usetime % 3600;
                    var m = parseInt(usetime / 60);
                    usetime = usetime % 60;
                    var mm = m;
                    var hh = h;
                    var ss = usetime;
                    if (m < 10) {
                        mm = "0" + m;
                    }
                    if (h < 10) {
                        hh = "0" + h;
                    }
                    if (usetime < 10) {
                        ss = "0" + usetime;
                    }
                    $("#danqianyongshi").html(hh + ":" + mm + ":" + ss);
                    answerImg = details;
                }
                else {
                    if (starTimes != null) {
                        clearTimeout(starTimes);
                    }
                    //启动计时
                    var h = parseInt(usetime / 3600);
                    usetime = usetime % 3600;
                    var m = parseInt(usetime / 60);
                    usetime = usetime % 60;
                    StarTimes(h, m, usetime + 1);
                }
                strHtml += "</div></li></ul>";
                break;
            }
        }
    }

    $("#gongjitishu").html(gongjitishu);
    $("#answers").html(answerImg);
    $("#question").html(strHtml);
}

var starTimes = null;

//启动计时
function StarTimes(h, m, usetime) {
    h += parseInt(usetime / 3600);
    usetime = usetime % 3600;
    m += parseInt(usetime / 60);
    usetime = usetime % 60;
    var mm = m;
    var hh = h;
    var ss = usetime;
    if (m < 10) {
        mm = "0" + m;
    }
    if (h < 10) {
        hh = "0" + h;
    }
    if (usetime < 10) {
        ss = "0" + usetime;
    }
    $("#danqianyongshi").html(hh + ":" + mm + ":" + ss);
    usetime++;
    starTimes = setTimeout("StarTimes(" + h + "," + m + "," + usetime + ")", 1000);
}


//题目数字
function GetNumberHtml(list, t, ifsubmit) {
    if (ifsubmit == 1) {
        $("#tongjidati").hide();
        $("#tongjijieguo").show();
    }
    else {
        $("#tongjidati").show();
        $("#tongjijieguo").hide();
    }
    var zhengquetishu = 0;
    var cuowutishu = 100;
    var gongjiyongshi = 0;
    var dangqiantihao = t;
    var yidatishu = 0;
    var hidAutoAnswer = $("#hidAutoAnswer").val();

    var titleNumber = "";
    for (var i = 0; i < list.length; i++) {
        var cid = list[i]["id"];
        if (i != 0 && i % 25 == 0) {
            titleNumber += "<div class=\"clearok\"></div>";
        }
        var chooseanswer = list[i]["chooseanswer"];
        var trueanswer = list[i]["trueanswer"];
        var usetime = list[i]["usetime"];
        gongjiyongshi += usetime;
        if (chooseanswer != null && chooseanswer != "") {
            yidatishu++;
            if (ifsubmit == 1) {
                if (chooseanswer == trueanswer) {
                    zhengquetishu++;
                    cuowutishu--;
                    titleNumber += "<div class=\"num ctrue\" onclick=\"ChooseQuestion(" + (i + 1) + ");\">" + (i + 1) + "</div>";
                }
                else {
                    titleNumber += "<div class=\"num cfalse\" onclick=\"ChooseQuestion(" + (i + 1) + ");\">" + (i + 1) + "</div>";
                }
            }
            else {
                if (t == cid) {
                    titleNumber += "<div class=\"num over\">" + (i + 1) + "</div>";
                }
                else {
                    titleNumber += "<div class=\"num out\" onclick=\"ChooseQuestion(" + (i + 1) + ");\">" + (i + 1) + "</div>";
                }
            }
        }
        else {
            if (ifsubmit == 1) {
                titleNumber += "<div class=\"num cfalse\" onclick=\"ChooseQuestion(" + (i + 1) + ");\">" + (i + 1) + "</div>";
            }
            else {
                if (t == cid) {
                    titleNumber += "<div class=\"num over\">" + (i + 1) + "</div>";
                }
                else {
                    titleNumber += "<div class=\"num\" onclick=\"ChooseQuestion(" + (i + 1) + ");\">" + (i + 1) + "</div>";
                }
            }
        }

    }
    $("#zhengquetishu").html(zhengquetishu);
    $("#cuowutishu").html(cuowutishu);

    var h = parseInt(gongjiyongshi / 3600);
    gongjiyongshi = gongjiyongshi % 3600;
    var m = parseInt(gongjiyongshi / 60);
    gongjiyongshi = gongjiyongshi % 60;
    var mm = m;
    var hh = h;
    var ss = gongjiyongshi;
    if (m < 10) {
        mm = "0" + m;
    }
    if (h < 10) {
        hh = "0" + h;
    }
    if (gongjiyongshi < 10) {
        ss = "0" + gongjiyongshi;
    }
    $("#gongjiyongshi").html(hh + ":" + mm + ":" + ss);
    $("#dangqiantihao").html(dangqiantihao);
    $("#yidatishu").html(yidatishu);
    $("#titleNumber").html(titleNumber);
}

//上一题下一题
function NextQuestion(t) {
    var chooseId = parseInt($("#hidQuestionID").val());
    if (t == -1) {
        if (chooseId > 1) {
            chooseId--;
        }
    }
    else {
        if (chooseId < 100) {
            chooseId++;
        }
    }

    $("#hidQuestionID").val(chooseId);
    var hidExamRoomID = $("#hidExamRoomID").val();
    GetNewExam(hidExamRoomID, chooseId);
}

//修改模拟题库
function UpdateExamRoom(id, t, n, v) {
    var list = examtxt;
    for (var i = 0; i < list.length; i++) {
        var exams = list[i];
        if (exams.id == id) {
            if (n == "ifsubmit") {
                exams.ifsubmit = v;
            }
            else {
                for (var j = 0; j < exams.list.length; j++) {
                    var result = exams.list[j];
                    if (result.id == t) {

                        if (n == "chooseanswer") {
                            result.chooseanswer = v;
                        }

                        if (n == "notebook") {
                            result.notebook = v;
                        }

                        if (n == "ifcollect") {
                            result.ifcollect = v;
                        }
                        if (n == "errorbook") {
                            result.errorbook = v;
                        }
                        if (n == "usetime") {
                            result.usetime = v;
                        }
                        break;
                    }
                }
            }
            break;
        }
    }
    examtxt = list;
}

//修改真题
function UpdateExamRoomT(id, t, n, v) {
    var list = examroomtxt;
    for (var i = 0; i < list.length; i++) {
        var exams = list[i];
        if (exams.id == id) {
            if (n == "ifsubmit") {
                exams.ifsubmit = v;
            }
            else {
                for (var j = 0; j < exams.list.length; j++) {
                    var result = exams.list[j];
                    if (result.id == t) {

                        if (n == "chooseanswer") {
                            result.chooseanswer = v;
                        }

                        if (n == "notebook") {
                            result.notebook = v;
                        }

                        if (n == "ifcollect") {
                            result.ifcollect = v;
                        }
                        if (n == "errorbook") {
                            result.errorbook = v;
                        }
                        if (n == "usetime") {
                            result.usetime = v;
                        }
                        break;
                    }
                }
            }
            break;
        }
    }
    examroomtxt = list;
}

//保存笔记
function SaveNote() {
    var id = $("#hidExamRoomID").val();
    var cid = $("#hidQuestionID").val();
    var note = $("#txtNote").val();
    var exam = $("#hidExams").val();
    if (exam == "2") {
        UpdateExamRoomT(id, cid, "notebook", note);
    }
    else {
        UpdateExamRoom(id, cid, "notebook", note);
    }
    alert("保存成功");
    CloseContentDiv();
    GetNewExam(id, cid);
}

//保存错误反馈
function SaveError() {
    var id = $("#hidExamRoomID").val();
    var cid = $("#hidQuestionID").val();
    var errorbook = $("#txtError").val();
    var exam = $("#hidExams").val();
    if (exam == "2") {
        UpdateExamRoomT(id, cid, "errorbook", errorbook);
    }
    else {
        UpdateExamRoom(id, cid, "errorbook", errorbook);
    }
    alert("保存成功");
    CloseContentDiv();
    GetNewExam(id, cid);
}

//查找结果答题
function GetSearchExam(id, cid) {
    CloseContentDiv();
    GetNewExam(id, cid);
}

//按条件模拟题查询
function SearchByTitle(id, t) {
    var searchResult = "";
    var list = examtxt;
    for (var i = 0; i < list.length; i++) {
        var exams = list[i];
        if (exams.id == id) {
            for (var j = 0; j < exams.list.length; j++) {
                var result = exams.list[j];
                if (result.question.indexOf(t) >= 0) {
                    searchResult += "<li class=\"btnDiv\" onclick=\"GetSearchExam(" + id + "," + result.id + ");\">" + result.question + "</li>";
                }
            }
        }
    }
    if (searchResult == "") {
        searchResult = "无数据";
    }
    $("#searchResult").html(searchResult);
}

//按条件真题查询
function SearchByTitleT(id, t) {
    var searchResult = "";
    var list = examroomtxt;
    for (var i = 0; i < list.length; i++) {
        var exams = list[i];
        if (exams.id == id) {
            for (var j = 0; j < exams.list.length; j++) {
                var result = exams.list[j];
                if (result.question.indexOf(t) >= 0) {
                    searchResult += "<li class=\"btnDiv\" onclick=\"GetSearchExam(" + id + "," + result.id + ");\">" + result.question + "</li>";
                }
            }
        }
    }
    if (searchResult == "") {
        searchResult = "无数据";
    }
    $("#searchResult").html(searchResult);
}

//查找题目
function Search() {
    var id = $("#hidExamRoomID").val();
    var title = $("#txtSearch").val();
    var exam = $("#hidExams").val();
    if (exam == "2") {
        SearchByTitleT(id, title);
    }
    else {
        SearchByTitle(id, title);
    }
    CloseContentDiv(3);
    return;
    $.ajax({
        type: "POST",
        url: "Service.ashx",
        dataType: "json",
        data: { action: "Search", id: id, title: encodeURI(title) },
        success: function (result) {
            if (result != null && result != "") {
                var cid = result[0]["id"];
                CloseContentDiv();
                GetNewExam(id, cid);
                $("#txtSearch").val("");
            }
        }
    });
}

//选择题号
function ChooseQuestion(t) {
    var id = $("#hidExamRoomID").val();
    GetNewExam(id, t);
}

//收藏
function Favorite() {
    var id = $("#hidExamRoomID").val();
    var cid = $("#hidQuestionID").val();
    var t = $("#hidFavorite").val();
    if (t == "0") {
        t = 1;
    }
    else {
        t = 0;
    }
    var exam = $("#hidExams").val();
    if (exam == "2") {
        UpdateExamRoomT(id, cid, "ifcollect", t);
    }
    else {
        UpdateExamRoom(id, cid, "ifcollect", t);
    }
    GetNewExam(id, cid);
}

//提交试卷
function SubmitAnswer() {
    var yidatishu = $("#yidatishu").html();
    var gongjitishu = $("#gongjitishu").html();
    if (yidatishu != gongjitishu) {
        var bln = confirm("还有未答的题目，交卷后不可修改，确定要交卷吗？")
        if (bln == false) {
            return;
        }
    }
    var ifsubmit = $("#hidIfSubmit").val();
    if (ifsubmit == "1") {
        alert("该试题已经提交，不可重复提交");
        return;
    }
    var id = $("#hidExamRoomID").val();
    var cid = $("#hidQuestionID").val();
    var exam = $("#hidExams").val();
    if (exam == "2") {
        UpdateExamRoomT(id, cid, "ifsubmit", 1);
    }
    else {
        UpdateExamRoom(id, cid, "ifsubmit", 1);
    }
    GetNewExam(id, 1);
    alert("交卷成功");
}

//答题
function Answer(t) {
    var ifsubmit = $("#hidIfSubmit").val();
    if (ifsubmit == "1") {
        alert("该试题已经提交，不可修改答案");
        return;
    }
    var id = $("#hidExamRoomID").val();
    var cid = $("#hidQuestionID").val();
    var usetime = 0;
    var danqianyongshi = $("#danqianyongshi").html();
    var usetimelist = danqianyongshi.split(':');
    usetime = parseInt(usetimelist[0]) * 3600 + parseInt(usetimelist[1]) * 60 + parseInt(usetimelist[2]);
    var exam = $("#hidExams").val();
    if (exam == "2") {
        UpdateExamRoomT(id, cid, "chooseanswer", t);
        UpdateExamRoomT(id, cid, "usetime", usetime);
    }
    else {
        UpdateExamRoom(id, cid, "chooseanswer", t);
        UpdateExamRoom(id, cid, "usetime", usetime);
    }
    var cbAutoAnswer = $("#cbAutoAnswer").attr("checked");
    if (cbAutoAnswer && parseInt(cid) < 100) {
        GetNewExam(id, parseInt(cid) + 1);
    }
    else {
        GetNewExam(id, cid);
    }
}